<!--平台-->
<template>
  <div class="admin-body">
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item >平台管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-row style="padding: 15px">

      <el-row class="main">
        <el-row  style="min-height: 40px;display: flex;">
          <el-col  style="margin-bottom: 5px;" align="left">
            <el-input v-model="key" clearable style="width: 300px;" placeholder="输入关键词查询" class="kz_search">
              <el-button slot="append" type="primary"  @click="getstart()">搜索</el-button>
            </el-input>
          </el-col>
          <el-col  style="margin-bottom: 5px;" align="right">
            <el-button type="primary" @click="add()" style="">新增</el-button>
          </el-col>
        </el-row>
        <!--表格-->
        <el-table
          select-on-indeterminate
          ref="multipleTable"
          border
          align="left"
          :data="tableData">
          <el-table-column
            prop="name"
            label="子平台名称">
          </el-table-column>
          <el-table-column
            prop="user_account"
            label="账号">
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button @click="changeMes(scope.row.platform_id,0)" style="color: red" size="small" type="text">
                删除
              </el-button>
              <el-button @click="edit(scope.row)"  style="" size="small" type="text">
                修改
              </el-button>
              <el-button @click="restPassword(scope.row.platform_id)"  style="" size="small" type="text">
                重置密码
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <el-row style="margin-top: 20px">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" align="right">
            <el-pagination
              style="text-align: right;margin-bottom:70px;"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="dataTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </el-row>
    </el-row>
    <el-dialog title="新增" :visible.sync="addVisible" width="360px" :modal-append-to-body="false">
      <el-form :model="adddata" :rules="addrules" ref="adddata">
        <el-form-item label="名称：" :label-width="formLabelWidth" prop="user_name">
          <el-input v-model="adddata.user_name" clearable></el-input>
        </el-form-item>
        <el-form-item label="账号：" :label-width="formLabelWidth" prop="user_account">
          <el-input  v-model="adddata.user_account"  clearable></el-input>
        </el-form-item>
        <el-form-item label="密码：" :label-width="formLabelWidth" prop="user_password">
          <el-input v-model="adddata.user_password"  type="password" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="SubmitAdd()">确 定</el-button>
        <el-button @click="addVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="editVisible" width="360px" :modal-append-to-body="false">
      <el-form :model="editdata" :rules="editrules" ref="adddata">
        <el-form-item label="名称：" :label-width="formLabelWidth" prop="user_name">
          <el-input v-model="editdata.user_name" clearable></el-input>
        </el-form-item>
        <el-form-item label="账号：" :label-width="formLabelWidth" prop="user_account">
          <el-input  v-model="editdata.user_account"  clearable></el-input>
        </el-form-item>
        <!--<el-form-item label="密码：" :label-width="formLabelWidth" prop="user_password">-->
          <!--<el-input  v-model="editdata.user_password"  clearable></el-input>-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="SubmitEdit()">确 定</el-button>
        <el-button @click="editVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="重置密码" :visible.sync="restVisible" width="360px" :modal-append-to-body="false">
      <el-form :model="restdata" :rules="restrules" ref="restdata">
        <el-form-item label="新密码：" :label-width="formLabelWidth" prop="new_password">
          <el-input v-model="restdata.new_password"  type="password" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="SubmitRest()">确 定</el-button>
        <el-button @click="restVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "Platform",
    mounted() {
      if (!this.$store.state.login.in) {
        this.$router.push({name: "login", params: {userId: 123}})
      }
      this.getstart()
    },
    data() {
      return {
        pageSize: 10,
        page: 1,
        dataTotal: 0,
        tableData:[],
        addVisible:false,
        formLabelWidth:'80px',
        adddata:{
          user_account:'',
          user_name:'',
          // user_password:'',
          permission:'',
        },
        addrules:{
          user_account: [
            {required: true, message: '请输入账号', trigger: 'blur'}
          ],
          user_name: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          // user_password: [
          //   {required: true, message: '请输入密码', trigger: 'blur'}
          // ],
        },
        editVisible:false,
        editdata:{
          platform_id:'',
          user_account:'',
          user_name:'',
          user_password:'',
          permission:'',
        },
        editrules:{
          user_account: [
            {required: true, message: '请输入账号', trigger: 'blur'}
          ],
          user_name: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
        },
        restVisible:false,
        restdata:{
          new_password:'',
          platform_id:'',
        },
        restrules:{
          new_password: [
            {required: true, message: '请输入新密码', trigger: 'blur'}
          ],
        },
        key:'',
      }
    },
    computed:{
      userdata(){
        return this.$store.state.login.user
      }
    },
    methods: {
      edit(data){
        console.log(data)
        this.editdata = JSON.parse(JSON.stringify(data))
        this.editdata.user_name = data.name
        this.editdata.platform_id = data.platform_id
        this.editVisible = true
      },
      restPassword(id){
        this.restVisible = true
        this.restdata.platform_id = id
      },
      SubmitEdit(){
        let thi = this
        if (thi.editdata.user_account == ''  || thi.editdata.user_name == ''){
          thi.$message({
            message: '请上传完整信息！',
            type: 'error',
          });
          return false
        }
        thi.editdata.permission = thi.$store.state.login.user.permission
        thi.mySubmit('edit', thi.editdata).then(function (code) {
          if (code==200) {
            thi.editVisible = false
            thi.getstart()
          }
        })
      },
      SubmitAdd(){
        let thi = this
        if (thi.adddata.user_account == '' || thi.adddata.user_password == '' || thi.adddata.user_name == ''){
          thi.$message({
            message: '请上传完整信息！',
            type: 'error',
          });
          return false
        }
        thi.adddata.permission = thi.$store.state.login.user.permission
        thi.mySubmit('register', thi.adddata).then(function (code) {
          if (code==200) {
            thi.addVisible = false
            thi.getstart()
          }
        })
      },
      SubmitRest(){
        let thi = this
        if (thi.restdata.new_password == ''){
          thi.$message({
            message: '请输入新密码！',
            type: 'error',
          });
          return false
        }
        thi.mySubmit('rPassWord', thi.restdata).then(function (code) {
          if (code==200) {
            thi.restVisible = false
            thi.getstart()
          }
        })
      },
      details(data){
        this.$store.state.login.platformId = data
        this.$router.push({name:'platformDetail'})
        console.log(this.$store.state.login.platformId)

      },
      add(){
        this.addVisible = true
      },
      //绑定表格数据，分页
      getstart() {
        let thi = this
        const prams = {
          page:thi.page,
          pageSize:thi.pageSize,
          key:thi.key,
        }
        thi.mySelectAll('platformList',prams).then(function (value) {
          thi.tableData = value.data
          thi.dataTotal = value.Total
        })
      },

      //选择展示数量
      handleSizeChange(val) {
        this.pageSize = val
        this.getstart()
      },
      //分页
      handleCurrentChange(val) {
        this.page = val
        this.getstart()
      },
      //状态改变提示
      changeMes(id, state) {
        var heeh = '该'
        var caozuo = '禁用'
        if (id.length > 1) {
          heeh = '这些'
        }
        if (state == 0) {
          caozuo = '删除'
        }
        let thi = this
        thi.$confirm('此操作将'+caozuo + heeh+'平台', '是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          thi.submitState(id,state)
        }).catch(() => {
          thi.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      //删除
      submitState(id,state) {
        let thi = this
        const por = {
          platform_id: id,
          state: state,
        }
        console.log(por)
        thi.mySubmit('platformState', por).then(function (code) {
          if (code == 200) {
            thi.getstart()
          }
        })
      },
    }
  }

</script>

<style scoped>
  .main {
    background-color: white;
    min-height: 500px;
    padding: 10px;
    border: #EDEDED 1px solid;
    border-radius: 5px;
  }
  .el-row{
    /*防止页面抖动*/
    width: calc(100% - 2px) !important;
  }
</style>
